﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using OpenGiving;

public partial class UserSkillsUserControl : System.Web.UI.UserControl
{
    private string _currentCategory = "";

    protected void Page_Load(object sender, EventArgs e)
    {
    }


    public void SaveSkillSet()
    {
        SaveSkillSet(Membership.GetUser().UserName);
    }

    public void SaveSkillSet(string username)
    {
        using (OpenGivingDataContext db = new OpenGivingDataContext())
        {
            try
            {
                string skillSet = GetSkillSet();
                MembershipUser u = Membership.GetUser(username);

                // Get mapping from DB since might not always be current user
                UserMappings map = db.UserMappings.Where(x => x.userID == (Guid)u.ProviderUserKey).FirstOrDefault();

                UserAttributes skill;

                foreach (string tuple in skillSet.Split(';'))
                {
                    short skill_id = Int16.Parse(tuple.Substring(0, tuple.IndexOf(":")));
                    short val = Int16.Parse(tuple.Substring(tuple.IndexOf(":") + 1));

                    skill = db.UserAttributes.Where(x => x.attribute_id == skill_id && x.contact_id == map.contact_id).FirstOrDefault();
                    if (skill == null)
                    {
                        skill = new UserAttributes();
                        db.UserAttributes.InsertOnSubmit(skill);
                    }
                    skill.contact_id = map.contact_id;
                    skill.attribute_id = skill_id;
                    skill.attributeValue = val;
                }

                db.SubmitChanges();
            }
            catch { }
        }
    }

    public string GetSkillSet()
    {
        //Dictionary<int, int> skills = new Dictionary<int, int>();
        StringBuilder skillString = new StringBuilder();

        foreach (Control c in ListView1.Items)
        {
            HiddenField skill = c.FindControl("attribute_id") as HiddenField;
            RadioButtonList skillValue = c.FindControl("attribute_level") as RadioButtonList;

            if (skill == null || skillValue == null)
            {
                continue;
            }
            
            //skills.Add(int.Parse(skill.Value), int.Parse(skillValue.Text));

            if (skillString.Length > 0) skillString.Append(";");
            skillString.AppendFormat("{0}:{1}", int.Parse(skill.Value), int.Parse(skillValue.Text));
        }

        //Session.Add("SkillSet", skillString.ToString());
        return skillString.ToString();
    }

    public void SetSkillSet(string skillSet)
    {
        string[] tuples = skillSet.Split(';');
        if (tuples.Length == 0) return;

        foreach (Control c in ListView1.Items)
        {
            HiddenField skill = c.FindControl("attribute_id") as HiddenField;
            RadioButtonList skillValue = c.FindControl("attribute_level") as RadioButtonList;

            int skill_id = int.Parse(skill.Value);
            string valPair = tuples.Where(x => x.StartsWith(skill_id + ":")).FirstOrDefault();
            if (valPair == null) skillValue.SelectedIndex = 0;
            else
            {
                string val = valPair.Substring(valPair.IndexOf(":") + 1);

                if (val == "0") skillValue.SelectedIndex = 0;
                else if (val == "3") skillValue.SelectedIndex = 1;
                else if (val == "5") skillValue.SelectedIndex = 2;
            }

        }
    }

    public string GetGroup(string template)
    {
        string category = Eval("category") as string;

        if (_currentCategory != category)
        {
            _currentCategory = category;
            return String.Format(template, category);
        }
        else
        {
            return "";
        }
    }

    protected void ListView1_DataBound(object sender, EventArgs e)
    {
        // For logged-in users, this will set the radio buttons to reflect skill levels
        if (Request.IsAuthenticated)
        {
            SetSkillSet(Profile.OpenGivingSkills);
        }
    }
}
